<?php
/*
 * This class is used for as Model for Blog table 
 */
class Model_Blog extends Model_Base {
    protected $_name = "tbl_blog";
    protected $_primaryKey = "blog_id";
     
    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("blog" => "tbl_blog"))
                ->where("blog_id = ?", $id)
                ->where("blog_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get blog
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchBlog($where = "", $limit = "", $offset = "0", $order = "blog_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("blog" => "tbl_blog"))
                ->where("blog_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getBlogCount($where) {
        $select = $this->select()
                ->from(array("blog_" => "tbl_blog"), Array("cnt" => "count(*)"))
                ->where("blog_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteBlog($blog_Array) {
        if (is_array($blog_Array)) {
            foreach ($blog_Array as $_blog) {
                $blogData = Array();
                $blogData = $this->fetchEntryById($_blog);
                if (count($blogData) > 0) {
                    $data = array("blog_status" => "2");
                    $this->update($data, "blog_id =" . (int) $_blog);
                }
            }
        } else {
            $blog__id = $blog_Array;
            $blogData = $this->fetchEntryById($blog__id);
            if (count($blogData) > 0) {
                $data = array("blog_status" => "2");
                $this->update($data, "blog_id =" . $blog__id);
            }
        }
    }

    public function fetchBlogWITHComment($where = "", $limit = "", $offset = "0", $order = "blog_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("blog" => "tbl_blog"))
                ->where("blog_status =1");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        $commentModel = new Model_Comment();
        $finalArray = array();
        if (!empty($data)) {
            $i = 0;
            foreach($data->toArray() as $key =>$value){
                $commnentData = $commentModel->fetchComment("cmt_blog_id = ".$value['blog_id'] );
                $finalArray[$i]['blog_id'] = $value['blog_id'];
                $finalArray[$i]['blog_title'] = $value['blog_title'];
                $finalArray[$i]['blog_shortdescription'] = $value['blog_shortdescription'];
                $finalArray[$i]['blog_description'] = $value['blog_description'];
                $finalArray[$i]['blog_createddate'] = $value['blog_createddate'];
                $finalArray[$i]['blog_updateddate'] = $value['blog_updateddate'];
                $finalArray[$i]['blog_status'] = $value['blog_status'];
                $finalArray[$i]['comment_count'] = count($commnentData);
            $i++;
                
            }
            return $finalArray;
        } else {
            return array();
        }
    }

     public function NEXTblogid($id) {
        $select = $this->select()
                ->from(array("blog" => "tbl_blog"),array("next_id" => 'min(blog_id)'))
                ->where("blog_id > ?", $id)
                ->where("blog_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
     public function PREVIOUSblogid($id) {
        $select = $this->select()
                ->from(array("blog" => "tbl_blog"),array("prev_id" => 'max(blog_id)'))
                ->where("blog_id < ?", $id)
                ->where("blog_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
}?>
